package com.mrzhou.file.controller;

import com.mrzhou.common.entity.file.FileTree;
import com.mrzhou.core.tool.api.R;
import com.mrzhou.file.service.IFileService;
import com.mrzhou.web.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * 文件下载及查看文件
 */
@RestController
@Api(tags = "文件下载, 查看可下载的文件树")
public class FileDownloadController {

    @Autowired
    private IFileService fileService;

    @Value("${system.file.path}")
    private String parentPath;

    @PostMapping("file-download")
    public R<String> downloadFile(@RequestParam("path")String path, HttpServletResponse response) throws URISyntaxException {
        File file = new File(new URI(path));
        // 传输文件
        FileUtil.fileUploadStream(file,response);
        return R.success("下载成功！"); // 使用绝对路径
    }

    @PostMapping("file-tree")
    @ApiOperation("获取文件树")
    public R<List<FileTree>> getFileNameList() {
        List<FileTree> trees = fileService.fileTreeListHtml(parentPath);
        for(FileTree tree: trees) {
            tree.setBackColor("#F5F5F5");
        }
        return R.data(trees, "获取成功！");
    }

    @PostMapping("/aws-upload")
    @ApiOperation("测试AWS上传")
    public R<String> awsUpload(MultipartFile file) {
        return R.data(fileService.awsUploadFile(file));
    }

    @PostMapping("/ecs-upload")
    @ApiOperation("测试ECS上传")
    public R<String> ecsUpolad(MultipartFile file) {
        return R.data(fileService.ecsUploadFile(file));
    }

    @PostMapping("ecs-list-objects")
    @ApiOperation("获取桶的对象集合")
    public R<Object> ecsListObjects() {
        return R.data(fileService.ecsListObjects());
    }

}
